<?php
	error_reporting(E_ERROR);
	error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
	require_once("../query/db_connection.php");
	
	$countryid="CO-" . uniqid();
	$countryname=$_POST["ccountry"];
	$countrycode=$_POST["ccountrycode"];
	$cityid= "CI-" . uniqid();
	$cityname= $_POST['ccity'];
	$czipcode= $_POST['czipcode'];
	
	$query1 = "INSERT INTO country(countryid,countryname,countrycode) VALUES('$countryid', '$countryname', '$countrycode')";
	if(!mysql_num_rows(mysql_query("SELECT countryid FROM country WHERE countryname='$countryname'"))){
		if(!mysql_query($query1, $con)){
			die('Error: '. mysql_error());
		}
	}
	
	$result = mysql_query("SELECT countryid FROM country WHERE countryname='$countryname'");
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	$query2 = "INSERT INTO city(cityid,cityname,countryid,zipcode) VALUES('$cityid', '$cityname', '$row[countryid]' ,'$czipcode')";

	if(!mysql_num_rows(mysql_query("SELECT cityid FROM city WHERE cityname='$cityname'"))){
		if(!mysql_query($query2, $con)){
			die('Error: '.mysql_error());
		}
	}
	echo "Saved!";
?>